/*******LJL 20150820*******/
var CurModleArray = [];

/****保存配置事件****/
function SaveModelAction() {
	if (AppendModelData() == true) {
		if (UpDataModelXml() == false) {
			return;
			//UpdataappXml();
		}
	}
}

/****显示添加厂家型号模板****/
function ShowAddFacModel() {
	$("#addr1Name").val("");
	$("#addr2Name").val("");
	$('#addr1,#addr2').removeAttr("placeholder"); //除去元素属性
	$('#devtype').removeAttr("validate");
	$('#model').removeAttr("readonly");
	$('#ctrltype').removeAttr("readonly");
	$('#devtype').removeAttr("readonly");
	$('#etype').removeAttr("readonly");
	$('#modelcode').removeAttr("readonly");
	$('#protocol').removeAttr("readonly");
	$("#modelcode").val("");
	$("#protocol").val("");
	$("#facname").val("");
	$('#conf').removeAttr("readonly");
	$('#conf').val('01');
	$("#conf option[value=01]").attr("selected", "selected");	
	$('#etypename').removeAttr("readonly");
	$('#etypename').removeAttr("disabled");
	$('#conf').removeAttr("disabled");
	$('#modelcode').removeAttr("disabled");
	$('#protocol').removeAttr("disabled");
	$('#model').removeAttr("disabled");
	$('#ctrltype').removeAttr("disabled");
	//$('#devtype').removeAttr("disabled");
	//$('#etype').removeAttr("disabled");
	$('#epAddModal_addbtn').removeAttr("onclick");
	$('#epAddModalLabelTitle').html("添加设备信息");
	$('#epAddModal_addbtn').text("添加");
	$('#epAddModal .error').removeClass("error");
	$('#epAddModal .text-error').remove();
	$('#epAddModal').modal('show');
}

/****显示编辑厂家名称模板****/
function ShowEditFacModel() {
	//var selectedModel = $("#myTab > li[class='active'] > a").attr("value");
	var selectedName = $("#myTab > li[class='active'] > a").text().split("(");
	selectedName[0] = $.trim(selectedName[0]);
	$("#facnameEdit").val(selectedName[0]);
	$('#epEditModal').modal('show');
	$("#facnameEdit").click();
}
/*修改厂家名称动作*/
function EditFacModalAction(){
	var selectedModel = $("#myTab > li[class='active'] > a").attr("value");
	var getEditName = $("#facnameEdit").val();
	var etype = $("#SelectElemType").children("option:selected").val(); 
		if (CurElemTypeArray.length != 0) {
		$.each(CurElemTypeArray, function (indexetype, itemetype) {
			if ((itemetype.etype) == (etype)) {
				var newmodel = itemetype.model;
				newmodel = $.makeArray(newmodel);
				if (typeof(newmodel) == "object" && newmodel.constructor == Array) {
					$.each(newmodel, function (indexmodel, itemmodel) {
						if(itemmodel.model == selectedModel){
							itemmodel.name = getEditName;
						}
					});
				}
			} 
		});
	}
	var data = GetCurModelArrayData(etype);
	data = $.makeArray(data);
	//LoadNavtabs(data);
	loadFacModNav(data);
	var AObj = $("#myTab > li > a");
	$.each(AObj,function(index,item){
		var model = $(item).attr("value");
		if(model==selectedModel){
			$(item).click();
			return;
		}
	});	 
	$('#epEditModal').modal('hide');
}

/**** 更新数据（点击Nva标签） ****/
function RefreshModelTabData(liid, obj) {
	var etype = $("#SelectElemType").val();
	var $this = $(obj);
	$("#modelType" + liid).siblings().removeClass("in active");
	$("#modelType" + liid).addClass("in active");
	$("#myTab").find(".active").removeClass("active");
	$this.parent().addClass("active");
	var curetype = $("#SelectElemType").val();
	var curmodel = $("#myTab").find(".active>a").attr("value");
	var cmddata = [];
	data = GetCurCmdArrayArrayData(curmodel);
	$('#table' + liid).bootstrapTable('load', {
		data : GetCurCmdArrayArrayData(curmodel)
	});
}

/****命令类型下拉框onChange事件****/
function cmdctypechange() {
	$('#CMDAddModal .text-error').remove();
	var curcmdctype = $('#cmdctype').val();
	var curoemid = $('#oemId').val();
	if (curcmdctype == '0') {
		InitMCMDForm('2');
	} else {
		$('#cmddtype').empty();
		if (GetCurrcTypeOemIdModelData(curcmdctype, curoemid)) {
			FillNoChoosedSelect(cmdOemIdModelMap, 'cmddtype');
			$('#cmddtype').val('00');
			$("#cmddtype option[value='00']").attr("selected", "selected");
			var cmddata = GetCurrcmdTypeMCMDData(curcmdctype, curoemid, '00');
			if (cmddata != false)
				$.fn.tables.bindObjData('MCMDtable', eval(cmddata));
			else
				InitMCMDForm('2');
		} else {
			var errorMsg = "该厂商型号目前不存在模板命令";
			$('#cmdctype').after("<span class='text-error'>" + errorMsg + "</span>");
			$("#cmddtypeTR").hide();
			InitMCMDForm('2');
		}
	}
}

/****设备类型onChange事件****/
function selectetypechange() {
	var curetype = $("#SelectElemType").val();
	var data = GetCurModelArrayData(curetype);
	data = $.makeArray(data);
	//LoadNavtabs(data);
	loadFacModSlect(data);
}

/****添加厂家型号事件****/
function AddFacModalAction() {
	var etype = $("#SelectElemType").children("option:selected").val(); 
	var etypename = $("#SelectElemType").children("option:selected").text();
	var facnum = $("#modelcode").val();
	var pronum = $("#protocol").val();
	var facname = $("#facname").val();
	var protype = $("#conf").val();
	if (protype == "02") {
		facnum = "00";
		pronum = "00";
		$('#modelcode').val('00');
		$('#protocol').val('00');
	}
	var ispass = validateRow('epAddModal');
	if (!ispass) {
		return false;
	}
	var model = "0x" + protype + etype + facnum + pronum;
	var modelidmax = GetModelIdMax();
	var curmodelid = parseInt(modelidmax) + 1;
	var flag = 0;
	var addr1Name=0;
	var addr2Name=0;
	if(document.getElementById('addr1Check').checked){

		 addr1Name=$("#addr1Name").val();
		 if(addr1Name == "" || addr1Name == null || addr1Name == undefined){
			 alert("请输入额外地址1的显示名称");
			 return;
		 }
	}
	else{
		 addr1Name=0;
	}
	if(document.getElementById('addr2Check').checked){

		addr2Name=$("#addr2Name").val();
		 if(addr2Name == "" || addr2Name == null || addr2Name == undefined){
			 alert("请输入额外地址2的显示名称");
			 return;
		 }
	}
	else{
		addr2Name=0;
	}
	
	var elemTypeleng = CurElemTypeArray.length;
	//数据加载到CurElemTypeArray对象中
	var ishassamemodel=0;
	if (CurElemTypeArray.length != 0) {
		$.each(CurElemTypeArray, function (indexetype, itemetype) {
			var etypearry = itemetype;
			if ((itemetype.etype) == (etype)) {
				var newmodel = itemetype.model;
				if (typeof(newmodel) == "object" && newmodel.constructor == Array) {
					$.each(newmodel, function (indexmodel, itemmodel) {
						if(itemmodel.model == model){
						alert("已经存在该厂家型号("+model+")数据，您可以直接在该型号下添加模板");
						ishassamemodel=1;
						return false;
						}
					});
					if(ishassamemodel==0){
						newmodel.push({
						"id" : curmodelid,
						"model" : model,
						"name" : facname,
						"addr1Name" : addr1Name,
						"addr2Name" : addr2Name,
						"cmd" : "0"
						});
						itemetype.model = newmodel;			
					}
				} else {
					if(newmodel.model == model){
						alert("已经存在该厂家型号(" + model +")数据，您可以直接在该型号下添加模板");
						ishassamemodel=1;
						return false;
						}
					else{
						newmodel = $.makeArray(newmodel);
						newmodel.push({
							"id" : curmodelid,
							"model" : model,
							"name" : facname,
							"addr1Name" : addr1Name,
							"addr2Name" : addr2Name,
							"cmd" : "0"
						});
						itemetype.model = newmodel;
					}
				}
			} else { //新增etype
				flag++;
				if (flag == elemTypeleng) {
					var tempmobj1 = [];
					tempmobj1 = ({
						"id" : curmodelid,
						"model" : model,
						"name" : facname,
						"addr1Name" : addr1Name,
						"addr2Name" : addr2Name,
						"cmd" : "0"
					});
					tempmobj1 = $.makeArray(tempmobj1);
					CurElemTypeArray.push({
						"etype" : etype,
						"name" : etypename,
						//"addr1Name" : addr1Name,
						//"addr2Name" : addr2Name,
						"model" : tempmobj1
					});

				}
			}
		});
	}
	else {
		var tempmobj2 = [];
		tempmobj2 = ({
			"id" : curmodelid,
			"model" : model,
			"name" : facname,
			"addr1Name" : addr1Name,
			"addr2Name" : addr2Name,
			"cmd" : "0"
		});
		tempmobj2 = $.makeArray(tempmobj2);
		CurElemTypeArray.push({
			"etype" : etype,
			"name" : etypename,
			//"addr1Name" : addr1Name,
			//"addr2Name" : addr2Name,
			"model" : tempmobj2
		});
	}
	var data = GetCurModelArrayData(etype);
	data = $.makeArray(data);
	//LoadNavtabs(data);
	loadFacModSlect(data);
	$('#epAddModal').modal('hide');
	locationNewMod(model);
}
/*定位到新添加的设备model*/
function locationNewMod(model){
	var facMdel = model.substr(6,2);
	$("#SelectFacModel  option[value='" + facMdel + "']").attr("selected", "selected");
	selectFacModChange();
	$("#myTab > li[class='active']").removeClass();
	$.each($("#myTab > li > a"),function(index,item){
		if($(item).attr("value")==model){
			$(item).parent("li").addClass("active");
		}
	});
}

/****显示删除厂商型号模板****/
function ShowFacModelDelModel() {
	var curetype = $("#SelectElemType").children("option:selected").val(); //元件类型
	InitFacModelSelecthtml(GetCurModelArrayData(curetype), 'FacModelDelselect');
	var curvalue = $('#myTab').find(".active>a").attr("href");
	curvalue = curvalue.substr(10);
	$("#FacModelDelselect").val(curvalue);
	$("#FacModelDelselect  option[value='" + curvalue + "']").attr("selected", "selected");
	$('#FacModelDelModal').modal('show');
}

/****删除厂家型号事件****/
function FacModelDelAction() {
	//var curmodel11 = $("#myTab").find(".active>a").val();
	//var curmodel22 = $("#myTab").find(".active>a").attr("value");
	var curmid = $("#FacModelDelselect").children("option:selected").val();
	if (isNaN(curmid))
		alert("没有数据");
	else {
		var etype = $("#SelectElemType").children("option:selected").val(); //元件类型
		//选中厂家型号从CurElemTypeArray中删除
		$.each(CurElemTypeArray, function (indexetype, itemetype) {
			var CurModelArray = itemetype.model;
			if (itemetype.etype == etype) {
				$.each(CurModelArray, function (indexmodel, itemmodel) {
					if (itemmodel.id == curmid) {
						CurModelArray.splice(indexmodel, 1);
						return false;
					}
				});
			}
		});
		//LoadNavtabs(GetCurModelArrayData(etype));
		loadFacModSlect(GetCurModelArrayData(etype)); 
		//loadFacModNav(GetCurModelArrayData(etype));
		$('#FacModelDelModal').modal('hide');
	}
}

/****初始化厂家型号删除模板下拉框****/
function InitFacModelSelecthtml(selectarry, selectId) {
	$("#" + selectId).empty();
	var selectedValue;
	var length = selectarry.length;
	$.each(selectarry, function (index, item) {
		if (index == 0)
			selectedValue = item.id;
		$("#" + selectId).append("<option value=" + item.id + ">" + item.name + "  ( " + item.model + ")</option>");
	});
	$("#" + selectId).val(selectedValue);
	$("#" + selectId + " option[value='" + selectedValue + "']").attr("selected", "selected");
}

/**** 协议类型下拉框onChange事件 ****/
function confChange() {
	var protype = $("#conf").val();
	if (protype == "02") {
		$('#modelcode').val('00');
		$('#protocol').val('00');
		$('#modelcode').attr("readonly", "true");
		$('#protocol').attr("readonly", "true");
	}
	if (protype == "01") {
		$('#modelcode').val('');
		$('#protocol').val('');
		$('#modelcode').removeAttr("readonly");
		$('#protocol').removeAttr("readonly");
	}
}

/**** 加载Nav标签 ****/
/*
function LoadNavtabs(model_data) //CurModelArray&&etype==curetype
{
	$("#myTab").find("li").remove();
	$('#myTabContent').empty();
	$.each(model_data, function (index, item) {
		var liid = 'modelType' + item.id;
		if (index == 0) {
			$("#myTab").append("<li class='active'><a href='#" + liid + "' data-toggle='tab' value='" + item.model + "' onclick=RefreshModelTabData('" + item.id + "',this)>" + item.name + " (" + item.model + ")</a></li>");
		} else {
			$("#myTab").append("<li><a href='#" + liid + "' data-toggle='tab' value='" + item.model + "' onclick=RefreshModelTabData('" + item.id + "',this)>" + item.name + " (" + item.model + ")</a></li>");
		}
		LoadeachTabContentDiv(index, item.id, liid,item.model, item.name); /////CurModelArray  index,id,liid,model
		var tableid = 'table' + item.id;
		tableIds.push(tableid);
	});
	LoadTables();
}
*/
/*获取厂家型号*/
function loadFacModSlect(data){ //data 某种设备（空调、窗帘）的数据
	$("#SelectFacModel").empty();
	$("#devListDiv").empty();
	if(data!=""&&data!=undefined){
		var facModArray = [];
		$.each(data, function (index, item) {
			if(item.model!=""&&item.model!=undefined){
				var model = item.model;
				var facMod = model.substr(6,2);
				facModArray.push(facMod);
			}
			var tableid = 'table' + item.id;
			tableIds.push(tableid);
		});
		facModArray = facModArray.unique();//数组去重复
		initFacModeSelect(facModArray);
		loadFacModNav(data);
	}else {
		refreshDevListDivHtml("");
	}
}
/*初始化厂家型号下拉框*/
function initFacModeSelect(facModArray){
	var optionHtml = '';
	for(var i = 0 ; i < facModArray.length ; i++ ){
		if(i == 0){
			optionHtml = '<option selected="selected" value="'+facModArray[i]+'">'+facModArray[i]+'</option>';
		}else {
			optionHtml = '<option value="'+facModArray[i]+'">'+facModArray[i]+'</option>';
		}
		$("#SelectFacModel").append(optionHtml);
	}
}
/*设备列表刷新*/
function refreshDevListDivHtml(devFacName){
	$("#devListDiv").empty();
	var optionHtml = '';
	var devListDivHtml = '<h4 style="margin-left:10px;margin-bottom:15px;">'+devFacName+'设备列表&nbsp;&nbsp;&nbsp;&nbsp;'+
		'<button class="btn-flat"  onclick="ShowAddFacModel()"  id="ModlebtnAdd">'+
			'<i class="icon-plus"></i>添加'+
		'</button>&nbsp;&nbsp;'+
		'<button  class="btn-flat success"  id="FacModelEdit" onClick="ShowEditFacModel()">'+
			'<i class="icon-edit"></i>编辑'+
		'</button>&nbsp;&nbsp;'+
		'<button  class="btn-flat danger"  id="FacModelDel" onClick="ShowFacModelDelModel()">'+
			'<i class="icon-remove"></i>删除'+
		'</button>'+
		'</h4>'+
		'<div class="well area"  style="margin-top:5px; background-color:#ffff; border:#ffff;" id="floor1" >'+
			'<ul class="nav nav-tabs" id="myTab" style=" border-bottom:#ffff">'+
			'</ul>'+
			'<div id="myTabContent" class="tab-content section">'+
			'</div>'+
		'</div>';
	$("#devListDiv").append(devListDivHtml);
}
/*加载设备列表标签栏*/
function loadFacModNav(data){
	$("#myTab").find("li").remove();
	$('#myTabContent').empty();
	var devFacName = $("#SelectFacModel option:selected").val();
	refreshDevListDivHtml(devFacName);
	var curFacMod = $("#SelectFacModel option:selected").val();
	//var etype = $("#SelectElemType option:selected").val();
	var firstNav = 0;
	if(data.length > 0 && data != undefined){
		$.each(data,function(index,item){
			var facMod = item.model.substr(6,2);
			if(curFacMod == facMod){
				var liid = 'modelType' + item.id;
				if(firstNav == 0){
					firstNav++;
					$("#myTab").append("<li class='active'><a href='#" + liid + "' data-toggle='tab' value='" + item.model + "' onclick=RefreshModelTabData('" + item.id + "',this)>" + item.name + " (" + item.model + ")</a></li>");
				}else {
					$("#myTab").append("<li><a href='#" + liid + "' data-toggle='tab' value='" + item.model + "' onclick=RefreshModelTabData('" + item.id + "',this)>" + item.name + " (" + item.model + ")</a></li>");
				}

				var liid = 'modelType' + item.id;
			}
			LoadeachTabContentDiv(index, item.id, liid,item.model, item.name);
		});
	}else {

	}
	LoadTables();
}
/*厂家型号列表change事件*/
function selectFacModChange(){
	var etype = $("#SelectElemType option:selected").val();
	var data = GetCurModelArrayData(etype);
	loadFacModNav(data);
}

/**** 加载Nav标签下个标签 ****/
function LoadeachTabContentDiv(num, modelid, liid, curmodel,modelname) ///////CurModelArray  index,id,liid,model
{
	var tabContent = "";
	if (num == 0)
		tabContent = tabContent + "<div class='row-fluid tab-pane fade in active' id='" + liid + "'>";
	else
		tabContent = tabContent + "<div class='row-fluid tab-pane fade' id='" + liid + "'>";
	tabContent = tabContent + "<form id='formtab" + modelid + "'>";
	tabContent = tabContent + "<div><h4 style='margin-left:10px;margin-bottom:15px;'>" + modelname + "(" + curmodel + ")模板列表&nbsp;&nbsp;&nbsp;&nbsp;<a role='button' class='btn-flat' onclick=GetCmdModel('" + curmodel + "')><i class='icon-plus'></i>添加</a>&nbsp;&nbsp;&nbsp;&nbsp;<a role='button'  class='btn-flat danger' onclick=DelModelInfo('" + modelid + "')><i class='icon-remove'></i>删除</a>&nbsp;&nbsp;</h4></div>";
	tabContent = tabContent + "<div ><table id='table" + modelid + "'></table></div>";
	tabContent = tabContent + "</form></div>";
	$("#myTabContent").append(tabContent);
}

/**** 删除模板事件 ****/
function DelModelInfo(modelid) {
	var currDelIds = "";
	var rows = $('#table' + modelid).bootstrapTable('getSelections');
	for (var i = 0; i < rows.length; i++) {
		currDelIds += rows[i].id + ',';
	}
	currDelIds = currDelIds.substring(0, currDelIds.length - 1);
	if (currDelIds == "" || currDelIds.length == 0) {
		alert("请选择您要删除的记录！");
		return;
	}
	$('#DeleteAllModal').removeClass('modalSence');
	$('#DeleteAllModal').modal('show');
	$("#DeleteAllModal_delbtn").attr("onclick", "DelModelAction('" + modelid + "','" + currDelIds + "');");
}

/**** 删除模板事件 ****/
function DelModelAction(modelid, currDelIds) {
	var Delrules = currDelIds.split(",");
	var etype = $("#SelectElemType").children("option:selected").val();
	for (var i = 0; i < Delrules.length; i++) {
		var curscesetid = Delrules[i];
		var isDelStatus = false;
		var curmodel = $("#myTab").find(".active>a").attr("value");
		$.each(CurElemTypeArray, function (indexetype, itemetype) {
			var CurModelArray = itemetype.model;
			if (typeof(CurModelArray) == "object" && CurModelArray.constructor == Array) { // n model
				if (itemetype.etype == etype) {
					$.each(CurModelArray, function (indexmodel, itemmodel) {
						if (itemmodel.model == curmodel) {
							var CurCmdArray = itemmodel.cmd;
							if (typeof(CurCmdArray) == "object" && CurCmdArray.constructor == Array) { // n cmd   curscesetid
								$.each(CurCmdArray, function (indexcmd, itemcmd) {
									if (itemcmd.id == curscesetid) {
										CurCmdArray.splice(indexcmd, 1);
										return false;
									}

								});
							} else { //1 cmd
								if (CurCmdArray.id == curscesetid) {
									CurCmdArray = [];
									itemmodel.cmd = CurCmdArray;
								}
							}
						}
					});
				}
			} else { // 1 model    CurModelArray
				if (itemetype.etype == etype) {
					if (CurModelArray.model == curmodel) {
						var CurCmdArray = CurModelArray.cmd;
						if (typeof(CurCmdArray) == "object" && CurCmdArray.constructor == Array) { // n cmd   curscesetid
							$.each(CurCmdArray, function (indexcmd, itemcmd) {
								if (itemcmd.id == curscesetid)
									CurCmdArray.splice(indexcmd, 1);
							});
						} else { //1 cmd
							if (CurCmdArray.id == curscesetid)
								CurCmdArray = "0";
						}
					}
				}
			}
		});
	}

	$("#DeleteAllModal_delbtn").removeAttr("onclick");
	$('#DeleteAllModal').modal('hide'); 
	var data = GetCurCmdArrayArrayData(curmodel);
	if (typeof(data) == "undefined")
		data = [];
	$('#table' + modelid).bootstrapTable('load', {
		data : data
	});
}

/***获取 编辑/查看 模板****/
function GetCmdModelEdit(cmdid) {
	var curmodel = $("#myTab").find(".active>a").attr("value");
	$('#oemId').val(curmodel);
	$('#CMDAddModal .input-small').removeClass("error");
	$('#CMDAddModal .text-error').remove();
	$("#cmddtypeTR").hide();
	$('#CMDAddModal').modal('show');
	FillSelect(cmdTypeMap, 'cmdctype');
	$("#CMDAddModal_addbtn").attr("onclick", "AddEditCMDAction('" + cmdid + "')");
	var cmdarray=GetCurCmdArrayArrayData(curmodel);
	var cmddata;
	var cmdmodelname;
	var cmdreadwtite;
	var cmdctype;
	$.each(cmdarray,function(index,item){
		if(item.id==cmdid){
			cmddata=item.i;
			cmdmodelname=item.modelName;
			cmdreadwtite=item.rw;
			cmdctype=item.ctype;
		}
	});
	$("#cmdctype").val(cmdctype);
	$("#cmdctype option[value=" + cmdctype + "]").attr("selected","selected")
	$("#cmdrw").val(cmdreadwtite);
	$("#cmdrw option[value=" + cmdreadwtite + "]").attr("selected","selected");
	
	$('#modelname').val(cmdmodelname);
	$.fn.tables.bindObjData('MCMDtable', cmddata);
	InitCCMDForm();
}

/****设备模板列表 编辑/查看 事件****/
function AddEditCMDAction(cmdid) {
	var etype = $("#SelectElemType").val();
	var model = $('#oemId').val();
	var cmdrw = $('#cmdrw').val();
	var cmdctype = $("#cmdctype").val();
	var modelname = $("#modelname").val();
	$("#cmdctype").disable="disable";
	$('#CMDAddModal').modal('hide');
	var ccmdTab = GetCMDTableResult('MCMDtable'); //获取修改的表格数据
	ccmdTab = ccmdTab.sort(function(a, b){  //按id排序
                    			return (a.id - b.id);
                			});
	var cmddata = GetCMDTableResult('CMDAddtable') //i数据  
	if(cmddata!='0'){
	cmddata = cmddata.sort(function(a, b){
                    			return (a.id - b.id);
                			});
		ccmdTab = $.merge(ccmdTab,cmddata);
	}
	var rw = $("#cmdrw").children("option:selected").val();
	var curmodel = $("#myTab").find(".active>a").attr("value");
	var cmddata = GetCurCmdArrayArrayData(curmodel);
	//编辑数据更新到CurElemTypeArray对象中
	$.each(CurElemTypeArray, function (indexetype, itemetype) {
		if (itemetype.etype == etype) {
			var CurModelArray = itemetype.model;
			if (typeof(CurModelArray) == "object" && CurModelArray.constructor == Array) { // n model
				$.each(CurModelArray, function (indexmodel, itemmodel) {
					if (itemmodel.model == curmodel) {
						var CurCmdArray = itemmodel.cmd;
						if (typeof(CurCmdArray) == "object" && CurCmdArray.constructor == Array) { // n cmd
							$.each(CurCmdArray, function (indexcmd, itemcmd) {
								if (itemcmd.id == cmdid) {
									itemmodel.cmd.push({
										"id" : cmdid,
										"ctype" : cmdctype,
										"rw" : rw,
										"modelName" : modelname,
										"i" : ccmdTab
									});
									itemmodel.cmd.splice(indexcmd, 1);
									return false;
								}
							});
						} else { // 1 cmd
							if (CurCmdArray.id == cmdid) {
								//itemmodel.cmd=[];
								var temp = [];
								temp.push({
									"id" : cmdid,
									"ctype" : cmdctype,
									"rw" : rw,
									"modelName" : modelname,
									"i" : ccmdTab
								});
								itemmodel.cmd = temp;

							}
						}
					}
				});
			} else { // 1 model
				if (CurModelArray.model == curmodel) {
					var CurCmdArray = CurModelArray.cmd;
					if (typeof(CurCmdArray) == "object" && CurCmdArray.constructor == Array) { // n cmd
						$.each(CurCmdArray, function (indexcmd, itemcmd) {
							if (itemcmd.id == cmdid) {
								CurModelArray.cmd.push({
									"id" : cmdid,
									"ctype" : cmdctype,
									"rw" : rw,
									"modelName" : modelname,
									"i" : ccmdTab
								});
								CurModelArray.cmd.splice(indexcmd, 1);
								return false;
							}
						});
					} else { // 1 cmd
						if (CurCmdArray.id == cmdid) {
							CurModelArray.cmd = [];
							CurModelArray.cmd.push({
								"id" : cmdid,
								"ctype" : cmdctype,
								"rw" : rw,
								"modelName" : modelname,
								"i" : ccmdTab
							});

						}
					}
				}
			}
		}
	});
	var curetype = $("#SelectElemType").children("option:selected").val();
	var cmddata = GetCurCmdArrayArrayData(model);
	cmddata = cmddata.sort(function(a, b){  //按id排序
                    			return (a.id - b.id);
                			});
	$.each(tableIds, function (n, value) {
		$('#' + value).bootstrapTable('load', {
			data : cmddata
		});
	});
}

/**** 添加命令模板事件****/
function CMDAddModalSaveAction() {
	var ccmddata = GetCMDTableResult('MCMDtable') //i数据
	ccmddata = ccmddata.sort(function(a, b){
                    			return (a.id - b.id);
                			});
	
	var cmddata = GetCMDTableResult('CMDAddtable') //i数据
	if(cmddata!='0'){
	cmddata = cmddata.sort(function(a, b){
                    			return (a.id - b.id);
                			});
	
	ccmddata = $.merge(ccmddata,cmddata);
	}
	
	var curmodel = $("#myTab").find(".active>a").attr("value");
	var curetype = $("#SelectElemType").children("option:selected").val();
	var curname = $("#SelectElemType").children("option:selected").text();
	var modelname = $("#modelname").val();
	var rw = $("#cmdrw").children("option:selected").val();
	var curctype = $('#cmdctype').children("option:selected").val();
	
	
	
	var cmdidMax = GetCmdIdMax();
	var curcmdid = parseInt(cmdidMax) + 1;
	var modelidmax = GetModelIdMax();
	var curmodelid = parseInt(modelidmax) + 1;
	curmodelid = curmodelid.toString();
	var curcmd = [];
	curmodel = $("#myTab").find(".active>a").attr("value");
	var flag = 0;
	var leng = CurElemTypeArray.length;
	
	$.each(CurElemTypeArray, function (indexetype, itemetype) {
		if (itemetype.etype == curetype) {
			if (itemetype.hasOwnProperty("model")) { // has model
				var CurModelArray = itemetype.model;
				if (typeof(CurModelArray) == "object" && CurModelArray.constructor == Array) { // n model
					$.each(CurModelArray, function (indexmodel, itemmodel) {
						if (itemmodel.model == curmodel) {
							if (itemmodel.hasOwnProperty("cmd")) { //has cmd
								var CurCmdArray = itemmodel.cmd;
								if (typeof(CurCmdArray) == "object" && CurCmdArray.constructor == Array) { // n cmd
									CurCmdArray.push({
										"id" : curcmdid,
										"ctype" : curctype,
										"rw" : rw,
										"modelName" : modelname,
										"i" : ccmddata
									});
								} else { //1 cmd
									if (CurCmdArray == "0") {
										itemmodel.cmd = ({
											"id" : curcmdid,
											"ctype" : curctype,
											"rw" : rw,
											"modelName" : modelname,
											"i" : ccmddata
										});
									} else {
										CurCmdArray = $.makeArray(CurCmdArray);
										CurCmdArray.push({
											"id" : curcmdid,
											"ctype" : curctype,
											"rw" : rw,
											"modelName" : modelname,
											"i" : ccmddata
										});
										itemmodel.cmd = CurCmdArray;
									}
								}
							} else { // no cmd
								var CurCmdArray = ({
									"id" : curcmdid,
									"ctype" : curctype,
									"rw" : rw,
									"modelName" : modelname,
									"i" : ccmddata
								});
								itemmodel.cmd=CurCmdArray;
								/*
								CurModelArray.splice(indexmodel, 1);
								itemetype.model.push({
									"id" : itemmodel.id,
									"model" : itemmodel.model,
									"cmd" : CurCmdArray,
									"name" : itemmodel.name,
									"addr1Name" : itemmodel.addr1Name,
									"addr2Name" : itemmodel.addr2Name
								});
								*/
							}
						}
					});
				} else { // 1 model
					if (CurModelArray.model == curmodel) {
						if (CurModelArray.hasOwnProperty("cmd")) { //has cmd
							var CurCmdArray = CurModelArray.cmd;
							if (typeof(CurCmdArray) == "object" && CurCmdArray.constructor == Array) { // n cmd
								CurCmdArray.push({
									"id" : curcmdid,
									"ctype" : curctype,
									"rw" : rw,
									"modelName" : modelname,
									"i" : ccmddata
								});
							} else { //1 cmd
								if (CurCmdArray == "0") {
									CurModelArray.cmd = ({
										"id" : curcmdid,
										"ctype" : curctype,
										"rw" : rw,
										"modelName" : modelname,
										"i" : ccmddata
									});
								} else {
									CurCmdArray = $.makeArray(CurCmdArray);
									CurCmdArray.push({
										"id" : curcmdid,
										"ctype" : curctype,
										"rw" : rw,
										"modelName" : modelname,
										"i" : ccmddata
									});
									CurModelArray.cmd = CurCmdArray;
								}
							}
						} else { // no cmd
							var CurCmdArray = ({
								"id" : curcmdid,
								"ctype" : curctype,
								"rw" : rw,
								"modelName" : modelname,
								"i" : ccmddata
							});
							CurModelArray.cmd=CurCmdArray;
							/*
							CurModelArray.splice(indexmodel, 1);
							itemetype.model.push({
								"id" : itemmodel.id,
								"model" : itemmodel.model,
								"cmd" : CurCmdArray
							});
							*/
						}
					}
				}
			} else { // no model 添加cmd
			}
		}
	});
	var etype = $("#SelectElemType").children("option:selected").val();
	var cmddata = GetCurCmdArrayArrayData(curmodel);
	$.each(tableIds, function (n, value) {
		$('#' + value).bootstrapTable('load', {
			data : cmddata
		});
	});
	$('#CMDAddModal').modal('hide');
}

/**** 加载表格数据 ****/
function LoadTables() {
	var tableIDLength = parseInt(tableIds.length);
	$.each(tableIds, function (n, value) {
		initTables(value);
	});
}

/**** 加载表格数据 ****/
function initTables(tabid) {
	var tid = tabid.substring(tabid.indexOf('table') + 5, tabid.length);
	var etype = $("#SelectElemType").val();
	var curmodel = $("#myTab").find(".active>a").attr("value");
	var initcmddata = GetCurCmdArrayArrayData(curmodel);
	initcmddata = initcmddata.sort(function(a, b){
                    			return (a.id - b.id);
                });
	$('#' + tabid).bootstrapTable({ //ModlebtnAdd		myTab
		data : initcmddata, //GetcurModelIArrayData(etype,tid),
		//height : 445,
		striped : true,
		dataType : "json",
		pagination : true,
		singleSelect : false,
		contentType : 'application/json',
		pageSize : 5,
		pageNumber : 1,
		pageList: [5,10, 25, 50, 100],
		showRefresh : false,
		showToggle : false,
		search : false, //不显示 搜索框
		showColumns : false, //不显示下拉框（选择显示的列）
		sidePagination : "client", //服务端请求
		columns : [{
				field : 'state',
				checkbox : true
			}, {
				field : 'id',
				title : '模板ID',
				width : '10%',
				align : 'center',
				valign : 'middle',
				sortable : false
			}, {
				field : 'modelName',
				title : '模板名称',
				width : '10%',
				align : 'center',
				valign : 'middle',
				sortable : false
			}, {
				field : 'atype',
				title : '命令模板属性',
				width : '20%',
				align : 'center',
				valign : 'middle',
				formatter : modelcmdpro,
				sortable : false
			}, {
				title : '命令模板信息',
				field : 'dchan',
				width : '40%',
				align : 'center',
				valign : 'middle',
				formatter : modelcmdinfo,
				sortable : false
			}, {
				title : '编辑/查看',
				field : 'operate',
				width : '20%',
				align : 'center',
				valign : 'middle',
				formatter : editandlook,
				sortable : false
			}
		]

	});
}

function modelcmdinfo(value, row, index) {
	return [JSON.stringify(row.i)
	].join('');
}

function modelcmdpro(value, row, index) {
	return ['ctype:' + row.ctype + '<br>rw:' + row.rw
	].join('');
}

function editandlook(value, row, index) {
	return '<a class="btn-flat gray" style="text-decoration:none" onclick="GetCmdModelEdit(\'' + row.id + '\')">编辑/查看</a>';
}

/****下拉框初始化****/
function FillSelect(paramTypemap, selectId) {
	$("#" + selectId).empty();
	var selectedValue;
	var length = paramTypemap.size();
	for (var i = 0; i < length; i++) {
		if (i == 0)
			selectedValue = paramTypemap.getKey(i);
		$("#" + selectId).append("<option value=" + paramTypemap.getKey(i) + ">" + paramTypemap.getValue(i) + "</option>");
	}
	$("#" + selectId).val(selectedValue);
	$("#" + selectId + " option[value='" + selectedValue + "']").attr("selected", "selected");
}

/****删除行****/
var CurObj='';
function deleteRow(obj) {
	var cmdName = $(obj).parent("td").parent("tr").find("td:eq(0) > input").val();
	$("#delCmdName").text(cmdName);
	$('#DeleteModalCmd').modal('show');
	CurObj = obj;
	$("#DeleteModalCmdAction").attr('onclick','DeleteModalCmdAction()');
}
function DeleteModalCmdAction(){
	$.fn.tables.deleteRow(CurObj);
	CurObj = '';
}

/**** 获取命令模板 *****/
function GetCmdModel(curmodelid) {
	$('#CMDAddModal .input-small').removeClass("error");
	$('#CMDAddModal .text-error').remove();
	$('#oemId').val(curmodelid); 
	$('#modelname').val('');
	$("#cmddtypeTR").hide();
	$('#CMDAddModal').modal('show');
	$("#CMDAddModal_addbtn").attr("onclick", "CMDAddModalSaveAction()");
	FillSelect(cmdTypeMap, 'cmdctype');
	cmdctypechange();
	//etype=2;
	InitCCMDForm();
}

function InitCCMDForm() {
	var trHtml = $("#CMDAddtable").find("tr").slice(1).first().html();
	$("#CMDAddtable").find("tbody").find("tr").remove();
	$("#CMDAddtable").append("<tr>" + trHtml + "</tr>");
}
/****获取设备类型下拉框数据****/
function GetCurrcTypeOemIdModelData(curcmdctype, curoemid) {
	curoemid = '00';
	var isINsert = false;
	if (InitMcmdArry.length == 0) {
		return false;
	} else {
		$.each(InitMcmdArry.param, function (index, item) {
			if (item.ctype == curcmdctype) {
				var oemidarry;
				oemidarry = item.oem;
				if (typeof(oemidarry) == "object" && oemidarry.constructor == Array) {
					$.each(oemidarry, function (indexoem, itemoem) {
						if (itemoem.oemId == curoemid) {
							var modelarry;
							modelarry = itemoem.model;
							if (typeof(modelarry) == "object" && modelarry.constructor == Array) {
								cmdOemIdModelMap = new Map();
								$.each(modelarry, function (indexmodel, itemmodel) {
									cmdOemIdModelMap.put(itemmodel.modelId, itemmodel.modelName);
									isINsert = true;
								})
							} else {
								isINsert = true;
								cmdOemIdModelMap = new Map();
								cmdOemIdModelMap.put(itemoem.model.modelId, itemoem.model.modelName);
							}
							return false;
						}
					})
				} else {
					if (item.oem.oemId == curoemid) {
						var modelarry;
						modelarry = item.oem.model;
						if (typeof(modelarry) == "object" && modelarry.constructor == Array) {
							cmdOemIdModelMap = new Map();
							$.each(modelarry, function (indexmodel, itemmodel) {
								cmdOemIdModelMap.put(itemmodel.modelId, itemmodel.modelName);
								isINsert = true;
							})
						} else {
							isINsert = true;
							cmdOemIdModelMap = new Map();
							cmdOemIdModelMap.put(item.oem.model.modelId, item.oem.model.modelName);
						}
						return false;
					}
				}
				return false;
			}
		})
		return isINsert;
	}
}

/****设备类型下拉框初始化****/
function FillNoChoosedSelect(paramTypemap, selectId) {
	$("#" + selectId).empty();
	var length = paramTypemap.size();
	for (var i = 0; i < length; i++) {
		$("#" + selectId).append("<option value=" + paramTypemap.getKey(i) + ">" + paramTypemap.getValue(i) + "</option>");
	}
}

/****获取模板cmd数据****/
function GetCurrcmdTypeMCMDData(curcmdctype, curoemid, curmodel) {
	curoemid = '00';
	curmodel = '00';
	if (curmodel == '' || curmodel == undefined)
		curmodel = '00';
	var isINsert = false;
	if (InitMcmdArry.length == 0) {
		return false;
	} else {
		var ers;
		$.each(InitMcmdArry.param, function (index, item) {
			if (item.ctype == curcmdctype) {
				var oemidarry;
				oemidarry = item.oem;
				if (typeof(oemidarry) == "object" && oemidarry.constructor == Array) {
					$.each(oemidarry, function (indexoem, itemoem) {
						if (itemoem.oemId == curoemid) {
							var modelarry;
							modelarry = itemoem.model;
							if (typeof(modelarry) == "object" && modelarry.constructor == Array) {
								$.each(modelarry, function (indexmodel, itemmodel) {
									if (itemmodel.modelId == curmodel) {
										isINsert = true;
										ers = itemmodel.cmd;
										return false;
									}
								})
							} else {
								if (itemoem.model.modelId == curmodel) {
									isINsert = true;
									ers = itemoem.model.cmd;
								}
							}
							return false;
						}
					})
				} else {
					if (item.oem.oemId == curoemid) {
						var modelarry;
						modelarry = item.oem.model;
						if (typeof(modelarry) == "object" && modelarry.constructor == Array) {
							$.each(modelarry, function (indexmodel, itemmodel) {
								if (itemmodel.modelId == curmodel) {
									isINsert = true;
									ers = itemmodel.cmd;
									return false;
								}
							})
						} else {
							if (item.oem.model.modelId == curmodel) {
								isINsert = true;
								ers = item.oem.model.cmd;
							}
						}
						return false;
					}
				}
				return false;
			}
		})
		if (isINsert == false) {
			return false;
		} else {
			return ers;
		}
	}
}

/****绑定数据为JSON对象****/
$.fn.tables.bindObjData = function (gridId, tbResult) {
	var jsons = tbResult;
	if (jsons == null) {
		return;
	}
	var trHtml = $("#" + gridId).find("tr").slice(1).first().html();
	$("#" + gridId).find("tbody").find("tr").remove();
	//遍历行结果
	for (var i = 0; i < jsons.length; i++) {
		var trnum = $("#" + gridId).find("tr").slice(1).length - 1;
		if (i > trnum) {
			if (i == 0)
				$("#" + gridId).append("<tr name='FirstTr'>" + trHtml + "</tr>");
			else
				$("#" + gridId).append("<tr>" + trHtml + "</tr>");
		}
		//遍历行中每一列的key
		$.each(jsons[i], function (key, val) {
			$("#" + gridId).find("tr").eq(i + 1).find("td").each(function () {
				var $child = $(this).children();
				if ($child.attr("name") == key || $child.attr("propertys") == key) {
					switch ($child.attr("type")) {
					case "text":
						$child.val(val);
						break;

					case "a":
					case "button":
						$child.attr("value", val);
						break;
					default:
					}
				}
			});
		});
	}
};

/****验证行****/
function validateRow(tableid) {
	var ispass = $.fn.tables.validateRow(tableid);
	return ispass;
}

/****验证行****/
$.fn.tables.validateRow = function (gridId) {
	var isPass = true; //是否验证通过
	$("#" + gridId).find("tr").slice(1).each(function () {
		$(this).find("td").each(function () {
			$('.text-error').remove();
			$(this).children().removeClass("error");
		});
	});
	$("#" + gridId).find("tr").slice(1).each(function () {
		$(this).find("td").each(function () {
			var $this = $(this).children();
			var validate = $this.attr("validate");
			if (validate == undefined || validate == '') {
				return true;
			}
			var rules = validate.split("|");
			for (var i = 0, ruleLength = rules.length; i < ruleLength; i++) {
				var method = rules[i];
				var ruleRegex = /^(.+?)\[(.+)\]$/;
				var parts = ruleRegex.exec(method);
				var param = null;
				if (parts) {
					method = parts[1];
					param = parts[2];
				}
				switch (method) {
				case "required":
					var errorMsg = "此字段为必填项!";
					if ($.trim($this.val()) == "" || $this.val() == "" || $this.val() == "null" || $this.val() == null) {
						isPass = false;
						$this.addClass('error').after("<span class='text-error'>" + errorMsg + "</span>");
					}
					break;
				case "integer":
					var errorMsg = "此字段必须为整数!";
					var integerRegex = /^\-?[0-9]+$/;

					var keyVal = $this.val();
					if (keyVal == undefined || keyVal == "" || keyVal == null || keyVal.length == 0) {
						console.log("nll");
					} else {
						if (!integerRegex.test($.trim($this.val()))) {
							isPass = false;
							$this.addClass('error').after("<span class='text-error'>" + errorMsg + "</span>");
						}
					}
					break;
				case "max_length":
					var errorMsg = "输入长度请勿超过" + param;
					var byteLen = function (s) {
						return s.replace(/[^\x00-\xff]/g, "--").length
					};
					if (!(byteLen($.trim($this.val())) <= parseInt(param, 10))) {
						isPass = false;
						$this.addClass('error').after("<span class='text-error'>" + errorMsg + "</span>");
					}
					break;
				case "less_than":
					var errorMsg = "输入小于" + param + "的整数";

					if (!(parseInt($.trim($this.val())) < parseInt(param))) {
						isPass = false;
						$this.addClass('error').after("<span class='text-error'>" + errorMsg + "</span>");
					}
					break;
				case "equal":
					var errorMsg = "此处只能输入值为" + param;
					var decimalRegex = /^\-?[0-9]*\.?[0-9]+$/;
					if ($.trim($this.val()) != param) {
						isPass = false;
						$this.addClass('error').after("<span class='text-error'>" + errorMsg + "</span>");
					}
					break;
				case "Lengthequal":
					var errorMsg = "此处必须输入" + param + "位";
					var val = $.trim($this.val());
					if (val.length != param) {
						isPass = false;
						$this.addClass('error').after("<span class='text-error'>" + errorMsg + "</span>");
					}
					break;
				case "isHex":
					var errorMsg = "此处只允许输入字母和数字位";
					var Regx = /^[A-Za-z0-9]*$/;
					if (!Regx.test($.trim($this.val()))) {
						isPass = false;
						$this.addClass('error').after("<span class='text-error'>" + errorMsg + "</span>");
					}
					break;
				default:
					break;
				}
				if (isPass == false)
					break;
			}
		});
	});
	return isPass;
};

/****根据厂家型号（model）字段获取命令模板cmd数据****/
function GetCurCmdArrayArrayData(curmodel) {
	var CurCmdArray = [];
	$.each(CurElemTypeArray, function (indexetype, itemetype) {
		if (itemetype.hasOwnProperty("model")) { //has model
			var CurModelArray = itemetype.model;
			if (typeof(CurModelArray) == "object" && CurModelArray.constructor == Array) { // n model
				$.each(CurModelArray, function (indexmodel, itemmodel) {
					if (itemmodel.model == curmodel) {
						//CurCmdArray=itemmodel.cmd;
						if (itemmodel.model == curmodel) {
							if (itemmodel.cmd == "0")
								CurCmdArray = [];
							else
								CurCmdArray = itemmodel.cmd;
						}
					}
				})
			} else { //1 model
				//CurCmdArray=CurModelArray.cmd;
				if (CurModelArray.model == curmodel) {
					if (CurModelArray.cmd == "0")
						CurCmdArray = [];
					else
						CurCmdArray = CurModelArray.cmd;
				}
			}
		} else { // no model
			CurCmdArray = [];
		}
	});
	return $.makeArray(CurCmdArray);
}

/****根据设备类型获取CurModelArray****/
function GetCurModelArrayData(etype) //GetCurModelArrayData
{
	var data = [];
	$.each(CurElemTypeArray, function (index, item) {
		if (item.etype == etype) {
			if (item.hasOwnProperty("model")) {
				data = item.model;
			}
		}
	});
	return $.makeArray(data);
}

/****获取model标签的最大id****/
function GetModelIdMax() {
	var modelidmax = 0;
	$.each(CurElemTypeArray, function (index, item) {
		if (item.hasOwnProperty("model")) {
			var curmodelarray = item.model;
			if (typeof(curmodelarray) == "object" && curmodelarray.constructor == Array) { // n model
				$.each(curmodelarray, function (indexmodel, itemmodel) {
					if (parseInt(itemmodel.id) > modelidmax)
						modelidmax = parseInt(itemmodel.id);
				});
			} else { // 1 model  curmodelarray
				if (curmodelarray.model == "0")
					modelidmax = 0;
				else {
					if (parseInt(curmodelarray.id) > modelidmax)
						modelidmax = parseInt(curmodelarray.id);
				}
			}
		} else { // no model
			modelidmax = 0;
		}
	});

	return modelidmax;
}

/****号获取最大cmdid****/
function GetCmdIdMax() {
	var cmdidmax = 0;
	$.each(CurElemTypeArray, function (indexetype, itemetype) {
		if (itemetype.hasOwnProperty("model")) {
			var curmodelarray = itemetype.model;
			if (typeof(curmodelarray) == "object" && curmodelarray.constructor == Array) { // n model
				$.each(curmodelarray, function (indexmodel, itemmodel) {
					if (itemmodel.hasOwnProperty("cmd")) {
						var newcmd = itemmodel.cmd;
						if (typeof(newcmd) == "object" && newcmd.constructor == Array) { // n cmd
							$.each(newcmd, function (indexnewcmd, itemnewcmd) {
								if (parseInt(itemnewcmd.id) > cmdidmax){
									cmdidmax = parseInt(itemnewcmd.id);
								}
							});
						} else { // 1 cmd
							if (newcmd == "0");
							else {
								if (parseInt(newcmd.id) > cmdidmax)
									cmdidmax = parseInt(newcmd.id);
							}
						}
					} 
					/*else {
						cmdidmax = 0;
					}
					*/
				})
			} else { // 1 model    curmodelarray
				if (curmodelarray.hasOwnProperty("cmd")) {
					var newcmd = curmodelarray.cmd;
					if (typeof(newcmd) == "object" && newcmd.constructor == Array) { // n cmd
						$.each(newcmd, function (indexnewcmd, itemnewcmd) {
							if (parseInt(itemnewcmd.id) > cmdidmax)
								cmdidmax = parseInt(itemnewcmd.id);
						});
					} else { // 1 cmd
						cmdidmax = newcmd.id;
					}
				} else {
					cmdidmax = 0;
				}
			}
		} else{
			cmdidmax = 0;
		}
	});
	return cmdidmax;
}

/****数组去重****/
Array.prototype.unique = function(){
	var res = [];
	var json = {};
	for(var i = 0; i < this.length; i++){
		if(!json[this[i]]){
			res.push(this[i]);
			json[this[i]] = 1;
		}
	}
	return res;
}

/****对象数组根据字段排序*****/
/*
function keysrt(key, desc) {
	return function (a, b) {
		return desc ? ~~(a[key] < b[key]) : ~~(a[key] > b[key]);
	}
}
*/
/****数组排序并去重****/
/*
function unique(arr, removeAll) {
	arr.sort(function (a, b) {
		return a - b
	}); //先排序
	var s = arr.join(',') + ',';
	arr = s.replace(/(\d+,)\1+/g, removeAll ? '' : '$1').replace(/,$/, '').split(',');
	for (var i = 0; i < arr.length; i++)
		arr[i] = parseInt(arr[i], 10); //重新转为数字
	return arr;
}
*/
